from django import template
from pirate_permissions import facade
from customtags.decorators import block


@block
def canI(context, nodelist, *args, **kwargs):

    user = template.Variable(kwargs['user']).resolve(context)
    permission_str = kwargs['permission_str']
    component = kwargs['component']

    generic_obj = kwargs.get('generic_obj', None)

    if generic_obj:
        generic_obj = template.Variable(generic_obj).resolve(context)

    if facade.canI(user, permission_str, component, related_obj):
        return nodelist.render(context)
    else:
        return None
